﻿namespace Fetches.Fetch.Utils.IdGenerator
{
    public class IdMeta
    {

        private static readonly IdMeta MaxPeak = new IdMeta((byte)10, (byte)20, (byte)30, (byte)2, (byte)1, (byte)1);

        private static readonly IdMeta MinGranularity = new IdMeta((byte)10, (byte)10, (byte)40, (byte)2, (byte)1, (byte)1);

        private byte _machineBits;

        private byte _seqBits;

        private byte _timeBits;

        private byte _genMethodBits;

        private byte _typeBits;

        private byte _versionBits;

        public IdMeta(byte machineBits, byte seqBits, byte timeBits, byte genMethodBits, byte typeBits, byte versionBits)
        {
            _machineBits = machineBits;
            _seqBits = seqBits;
            _timeBits = timeBits;
            _genMethodBits = genMethodBits;
            _typeBits = typeBits;
            _versionBits = versionBits;
        }

        public long GetMachineBitsMask()
        {
            return -1L ^ -1L << _machineBits;
        }

        public int GetSeqBitsStartPos()
        {
            return _machineBits;
        }

        public long GetSeqBitsMask()
        {
            return -1L ^ -1L << _seqBits;
        }

        public byte GetTimeBits()
        {
            return _timeBits;
        }

        public int GetTimeBitsStartPos()
        {
            return _machineBits + _seqBits;
        }

        public long GetTimeBitsMask()
        {
            return -1L ^ -1L << _timeBits;
        }

        public int GetGenMethodBitsStartPos()
        {
            return _machineBits + _seqBits + _timeBits;
        }

        public long GetGenMethodBitsMask()
        {
            return -1L ^ -1L << _genMethodBits;
        }

        public int GetTypeBitsStartPos()
        {
            return _machineBits + _seqBits + _timeBits + _genMethodBits;
        }

        public long GetTypeBitsMask()
        {
            return -1L ^ -1L << _typeBits;
        }

        public int GetVersionBitsStartPos()
        {
            return _machineBits + _seqBits + _timeBits + _genMethodBits + _typeBits;
        }

        public long GetVersionBitsMask()
        {
            return -1L ^ -1L << _versionBits;
        }

        public static IdMeta GetIdMeta(IdType type)
        {
            if (type==IdType.Seconds)
            {
                return MaxPeak;
            }

            return MinGranularity;
        }
    }
}
